<?php

namespace apiend\controllers;

use Yii;
use apiend\components\Controller;
use common\base\ModelHttpException;
use common\logics\RouterLogic;
use common\models\app\AppVersion;
use common\models\app\AppSplash;
use apiend\models\DeviceForm;
use common\models\WebviewMap;
use apiend\models\SystemHelpSearch;

/**
 * 全局应用接口
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class GlobalController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['GET'],
            'register' => ['POST'],
        ];
    }

    /**
     * 获取H5前端路由地址别名
     * @return mixed
     */
    public function actionFrontend() {
        return RouterLogic::frontend();
    }

    /**
     * 获取H5前端路由地址别名
     * @return mixed
     */
    public function actionWebview() {
        return WebviewMap::findActive()->all();
    }

    /**
     * 获取H5前端路由地址别名
     * @return mixed
     */
    public function actionBaselink() {
        $help = new SystemHelpSearch();
        return $help->search();
    }

    /**
     * 获取应用版本升级
     * @return mixed
     */
    public function actionVersion() {
        return AppVersion::getLatest();
    }

    /**
     * 获取应用开屏动画
     * @return mixed
     */
    public function actionBootsplash() {
        return AppSplash::findRecent();
    }

    /**
     * 注册设备信息（推送消息时使用）
     * @return mixed
     */
    public function actionRegister() {
        $model = new DeviceForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            throw new ModelHttpException('提交时数据未加载成功');
        }
        $model->device = $this->token->device;
        return $model->register();
    }

    /**
     * 全局默认设置接口
     * @return mixed
     */
    public function actionSetting() {
        return [
            'link' => Yii::$app->id,
            'name' => Yii::$app->name,
        ];
    }

    /**
     * 获取H5前端路由地址别名
     * @return mixed
     */
    public function actionAddress() {
        return \common\models\base\BaseDistrict::loadDist();
    }

}
